/**  
 * @author renbangjie renbangjie@126.com  
 * @date 2015-8-28 下午2:31:55
 * @version V1.0  
 * <b>Copyright (c)</b> 2014医信金融信息服务（上海）有限公司-版权所有<br/>
 */
package com.yjd.comm.formater;

import java.util.HashSet;
import java.util.Set;

import org.springframework.format.AnnotationFormatterFactory;
import org.springframework.format.Parser;
import org.springframework.format.Printer;

import com.yjd.comm.annotation.DateString;
import com.yjd.comm.util.DateUtil;

/**
 * 日期处理器
 * 
 * @author renbangjie renbangjie@126.com
 * @date 2015-8-28 下午2:31:55
 */
public final class DateStringFormaterHandler implements
		AnnotationFormatterFactory<DateString> {

	@Override
	public Set<Class<?>> getFieldTypes() {
		Set<Class<?>> fields = new HashSet<Class<?>>();
		fields.add(String.class);
		return fields;
	}

	@Override
	public Printer<String> getPrinter(DateString annotation, Class<?> fieldType) {
		return configureFormatterFrom(annotation, fieldType);
	}

	@Override
	public Parser<String> getParser(DateString annotation, Class<?> fieldType) {
		return configureFormatterFrom(annotation, fieldType);
	}

	private DateFormatter configureFormatterFrom(DateString annotation,
			Class<?> fieldType) {
		if (!annotation.pattern().isEmpty()) {
			return new DateFormatter(annotation.pattern(),
					DateUtil.PATTERN_yyyyMMdd);
		}
		return new DateFormatter(DateUtil.PATTERN_yyyy_MM_dd,
				DateUtil.PATTERN_yyyyMMdd);
	}

}
